package com.yuyou.tas.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.tas.report.service.IExamModelService;
import com.yuyou.tas.report.service.IExamModelWordService;
import com.yuyou.tas.report.vo.req.ExamModelWordReq;
import com.yuyou.tas.report.vo.resp.ExamModelWordResp;
import com.yuyou.tas.util.bean.*;
import com.yuyou.tas.util.tool.SpringValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="ExamModelWordController",description="试卷达标文案接口")
@Validated
@RestController
@RequestMapping(value = "/api/examModelWord/")
public class ExamModelWordController extends BaseController{


    @Autowired
    private IExamModelService examModelService;

    @Autowired
    private IExamModelWordService examModelWordService;



    @ApiOperation(value = "文案列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "文案名称", paramType = "query"),
            @ApiImplicitParam(name = "isUse", value = "是否启用", paramType = "query")})
    @RequestMapping(value = "/wordList", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<ExamModelWordResp> wordList(@Validated @ModelAttribute PageParams pageParams,
                                                    @RequestParam(value = "name" , required = false) String name,
                                                    @RequestParam(value = "isUse", required = false) Integer isUse,
                                                    HttpServletRequest request) {
        LoginRedisObj user = this.getUserObj(request);
        Map<String, Object> conditions = new HashMap<>();
        Pagination<ExamModelWordResp> queryPgn = new Pagination<>();
        conditions.put("organId", user.getOrganizationId());
        conditions.put("name", StringUtils.isEmpty(name) ? name:name.trim());
        conditions.put("isUse", isUse);
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total = examModelWordService.selectExamModelWordsCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<ExamModelWordResp> list = examModelWordService.selectExamModelWords(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }

    @ApiOperation(value = "保存文案")
    @ApiImplicitParam(name = "json", value = "json字符串", paramType = "query")
    @RequestMapping(value = "/addWord", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<Void> addWord(@Validated @NotBlank(message = "json不能为空") @RequestParam(value = "json")String json,
                                                HttpServletRequest request) {
        LoginRedisObj user = this.getUserObj(request);
        ExamModelWordReq examModelWordReq = JSONObject.parseObject(json, new TypeReference<ExamModelWordReq>(){});
        ResBoolSimpleInfo<String> res= SpringValidateUtil.check(examModelWordReq);
        if(!res.isSuccess()){
            return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());
        }

        examModelWordService.addExamModelWord(examModelWordReq , user);
        return ReturnResult.success();
    }

    @ApiOperation(value = "更新文案")
    @RequestMapping(value = "/updateWord", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<Void> updateWord(@Validated @NotBlank(message = "json不能为空") @RequestParam(value = "json")String json,
                                                       HttpServletRequest request) {
        LoginRedisObj user = this.getUserObj(request);
        ExamModelWordReq examModelWordReq = JSONObject.parseObject(json, new TypeReference<ExamModelWordReq>(){});
        ResBoolSimpleInfo<String> res= SpringValidateUtil.check(examModelWordReq);
        if(!res.isSuccess()){
            return ReturnResult.returnRes(res.isSuccess(), res.getMessage(), res.getData());
        }else if(examModelWordReq.getId() == null){
            return ReturnResult.fail("ID不能为空");
        }
        examModelWordService.updateExamModelWord(examModelWordReq , user);
        return ReturnResult.success();
    }

    @ApiOperation(value = "更改文案状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文案ID", paramType = "query" , required = true),
            @ApiImplicitParam(name = "isUse", value = "是否启用", paramType = "query" , required = true)})
    @RequestMapping(value = "/changeWordStatus", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<Void> changeWordStatus(@Validated @NotNull(message = "文案ID不能为空") @RequestParam(value = "id") Long id,
                                                        @NotNull(message = "状态不能为空")@RequestParam(value = "isUse") Integer isUse,
                                                       HttpServletRequest request) {
        LoginRedisObj user = this.getUserObj(request);
        examModelWordService.changeExamModelWordStatus(id , isUse , user.getUserId());
        return ReturnResult.success();
    }

    @ApiOperation(value = "获取文案详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文案ID", paramType = "query" , required = true)})
    @RequestMapping(value = "/wordDetail", method = { RequestMethod.GET,RequestMethod.POST })
    public ReturnResult<ExamModelWordResp> wordDetail(@Validated @NotNull(message = "文案ID不能为空") @RequestParam(value = "id") Long id) {
        ExamModelWordResp resp = examModelWordService.selectExamModelWordById(id);
        return ReturnResult.success(resp);
    }

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
